//
//  AIBaseAlertView.m
//  Mia
//
//  Created by Vill on 2024/1/17.
//

#import "AIBaseAlertView.h"

@interface AIBaseAlertView ()

@property (nonatomic,assign) BOOL isAppear;

@end

@implementation AIBaseAlertView

-(id)init{
    self = [super init];
    if (self) {
        self.animationType = Center;
        self.contentWidth = KScreenWidth - PTW(30);
        self.blankView = [[UIView alloc] init];
        [self.blankView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blankViewTap)]];
        self.blankView.backgroundColor = [UIColor colorHex:@"#000000" alpha:0.5];
        [self addSubview:self.blankView];
        [self.blankView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
        self.contentView = [[UIView alloc] init];
        self.contentView.layer.masksToBounds = YES;
        self.contentView.layer.cornerRadius = PTW(20);
        self.contentView.backgroundColor = UIColor.clearColor;
        [self addSubview:self.contentView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

-(void)setContentWidth:(CGFloat)contentWidth{
    _contentWidth = contentWidth;
    if (self.contentView) {
        [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.contentWidth);
        }];
    }
}

-(void)showInView:(UIView *)view{
    self.isAppear = YES;
    self.alpha = 1;
    [view endEditing:YES];
    [view addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    switch (self.animationType) {
        case Center:{
            [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.centerY.mas_equalTo(self).mas_offset(-PTW(10));
                make.width.mas_equalTo(self.contentWidth);
            }];
        }
            break;
        case Top:{
            [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self).mas_offset(KStatusBarHeight);
                make.left.mas_equalTo(10);
                make.right.mas_equalTo(-10);
            }];
        }
            break;
        case Bottom:{
            [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(self).mas_offset(-KStatusBarHeight);
                make.left.mas_equalTo(10);
                make.right.mas_equalTo(-10);
            }];
        }
            break;
    }
    
    CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    popAnimation.duration = 0.4;
    popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DIdentity]];
    popAnimation.keyTimes = @[@0.0f, @0.5f, @0.75f, @1.0f];
    popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.contentView.layer addAnimation:popAnimation forKey:nil];
}

-(void)blankViewTap{
    [self dismiss];
    if (self.closeBlock){
        self.closeBlock();
    }
}

-(void)dismiss{
    self.isAppear = NO;
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    if (self.dismissBlock) {
        self.dismissBlock();
    }
}

-(void)keyboardWillShow:(NSNotification *)noti{
    if (self.animationType != Center) {
        return;
    }
    __weak typeof(self) wself = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [wself.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(wself);
            make.width.mas_equalTo(self.contentWidth);
            make.top.mas_equalTo(self).mas_offset(KStatusBarHeight);
        }];
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [wself layoutIfNeeded];
        } completion:^(BOOL finished) {
            
        }];
    });
}

-(void)keyboardWillHide:(NSNotification *)noti{
    if (self.animationType != Center) {
        return;
    }
    __weak typeof(self) wself = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [wself.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(wself);
            make.width.mas_equalTo(self.contentWidth);
        }];
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [wself layoutIfNeeded];
        } completion:^(BOOL finished) {
            
        }];
    });
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
